﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;

namespace DAL
{
    public class DocumentDAL : SqlDataProvider
    {
        #region[Document_GetById]
        public List<Document> Document_GetById(string Id)
        {
            List<Entity.Document> list = new List<Entity.Document>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_GetById", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Document.DocumentIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Document_GetByTop]
        public List<Document> Document_GetByTop(string Top, string Where, string Order)
        {
            List<Entity.Document> list = new List<Entity.Document>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_GetByTop", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Document.DocumentIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Document_GetByAll]
        public List<Document> Document_GetByAll(string Lang)
        {
            List<Entity.Document> list = new List<Entity.Document>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_GetByAll", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Document.DocumentIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Document_Paging]
        public List<Document> Document_Paging(string CurentPage, string PageSize, string Lang)
        {
            List<Entity.Document> list = new List<Entity.Document>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_Paging", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Document.DocumentIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Document_Insert]
        public bool Document_Insert(Document data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Code", data.Code));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@CreateDate", data.CreateDate));
                dbCmd.Parameters.Add(new SqlParameter("@EffectiveDate", data.EffectiveDate));
                dbCmd.Parameters.Add(new SqlParameter("@Info", data.Info));
                dbCmd.Parameters.Add(new SqlParameter("@File", data.File));
                dbCmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@TypeId", data.TypeId == "" || data.TypeId == "0" ? DBNull.Value : (object)data.TypeId));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Document");
            return true;
        }
        #endregion
        #region[Document_Update]
        public bool Document_Update(Document data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Code", data.Code));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@CreateDate", data.CreateDate));
                dbCmd.Parameters.Add(new SqlParameter("@EffectiveDate", data.EffectiveDate));
                dbCmd.Parameters.Add(new SqlParameter("@Info", data.Info));
                dbCmd.Parameters.Add(new SqlParameter("@File", data.File));
                dbCmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@TypeId", data.TypeId == "" || data.TypeId == "0" ? DBNull.Value : (object)data.TypeId));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Document");
            return true;
        }
        #endregion
        #region[Document_Delete]
        public bool Document_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Document_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Document");
            return true;
        }
        #endregion

    }
}
